;-------------------------------------------------------------------------------------------------------
;
;	This function creates a blank linLog plot object, on which box plots are created by extensive
;	use of gsn_add_polyline. The user can draw as many or as few box plots as they wish.
;	The plot is not drawn, and the frame is not advanced. May be used with gsn_panel.
;	The function options are as follows:
;
;	wks     ----- The workstation
;	x[*]    ----- A one-dimensional array containing the X-axis values of where the box plot(s)
;	              will be drawn.
;	y[*][*] ----- A two-dimensional array, where the rightmost dimension contains the box plot
;	 	      reference pt. data. y(n,0)=bottom_value, y(n,1)=bottom_value_of_box,
;		      y(n,2)=mid-value_of_box,y(n,3)=top_value_of_box,y(n,4)=top_value
;	boxOpts ----- 2 options attached as attributes may be attached here. 
;		      boxWidth ---- Sets the width of the box.
;		      boxColors ---- An array that contains the colors that the boxes will be drawn in.
;			             Ex. boxOpts@boxColors =  (/"green","blue"/)
;				     If the number of colors specified does not equal the number of 
;				     boxes drawn, only the first color specified will be used.
;	plotres ----- An optional xy-plot resource list. Will override defaults where applicable.
;	lineres ----- An optional resource list pertaining to the lines used to draw the boxes/lines.
;
;	Example call: plot3 = boxplot(wks,ntime,newyval,opti,res,False)
;		      draw(plot3)
;		      frame(wks)
;
;-------------------------------------------------------------------------------------------------------
 



;-------------------------------------------------------------------------------------------------------
; boxplot2
;-------------------------------------------------------------------------------------------------------

undef("boxplot2")

function boxplot2 (wks:graphic,x[*]:numeric,y[*][*]:numeric,boxOpts:logical,plotres:logical,lineres:logical,plotlegend:logical)

begin
	dimquery = dimsizes(y)
	numbox = dimquery(0)
	if (numbox.ne.dimsizes(x)) then
	   print("Fatal: X must be one-dimensional and both X and Y must have the same rightmost dimension")
	   exit
	end if

;----------------------------------------------
;	Developing x-axis
;----------------------------------------------
	xAxis = new(numbox+2,typeof(x))
	xAxis(1:numbox) = x
	if (numbox.ne.1) then
	   dx = x(1)-x(0)
	   xAxis(0) = x(0)-dx
	   xAxis(numbox+1) = x(numbox-1)+dx
	else
	   dx = 1
	   xAxis(0) = x-dx
	   xAxis(2) = x+dx
	end if
	
	if (boxOpts) then
	   if (isatt(boxOpts,"boxWidth")) then
	      boxwidth = boxOpts@boxWidth
	   else
	      boxwidth = dx*.3
	   end if
	else
	   boxwidth = dx*.3
	end if	
	
		
	labarr = new(numbox+2,"string")		;Prepare actual X-axis labels...
	labarr(0) = ""
	labarr(numbox+1) = ""
	labarr(1:numbox) = xAxis(1:numbox)	

;----------------------------------------------
; Whether to maximize plot in frame.
;----------------------------------------------

    maximize = get_res_value(plotres,"gsnMaximize",False) 

	if (plotres) then
	   print("Plot resources detected, accepting")
	   fsatts = getvaratts(plotres)
	   do ty = 0,dimsizes(fsatts)-1
	     if (fsatts(ty).eq."tmXBLabels") then   ;Special section to test for XBLabels
	       if (dimsizes(plotres@tmXBLabels).ne.numbox) then
		    print("Fatal:Number of XB Labels does not match number of boxes, exiting")
		    exit
		   else
		    labarr(1:numbox) = plotres@$fsatts(ty)$
		   end if
		   xblab = plotres@tmXBLabels
		   delete(plotres@tmXBLabels)  ;Delete so tmXBLabels is not used when all
	     end if                        ;atts are assigned below...
	   end do
	   delete(fsatts)
	end if
	
;----------------------------------------------
; plot settings 
;----------------------------------------------

   ;plot = create "plot" logLinPlotClass wks
	plot = create "plot" xyPlotClass wks
	   "trYMinF" : min(y)-2
	   "trYMaxF" : max(y)+2
	   "trXMinF" : min(xAxis)
	   "trXMaxF" : max(xAxis)
	   "pmTickMarkDisplayMode" : "Always"
	   "tmXBMode"   : "Explicit"
	   "tmXBValues" : xAxis
	   "tmXBLabels" : labarr
	   "tmYROn"  : False
	   "tmXTOn"  : False
	   "tmYRBorderOn" : False
	   "tmXTBorderOn" : False
  	   "pmTitleDisplayMode": "Always"                   ; allow titles
  	   "tiMainOn"     : True
  	   "tiMainString"   : ""	   
	end create
	if (plotres) then
	   attsetvalues(plot,plotres)	
	end if

;----------------------------------------------
; kai 		
;----------------------------------------------

 
;----------------------------------------------
; kai 		
;----------------------------------------------

	polyres = True						;Set up defaults
 	polyres@gsLineColor       = "black" ; color of lines
    polyres@gsLineThicknessF  = 1.0     ; thickness of lines
	polyres@gsLineDashPattern = 0
	if (lineres) then
	   fsatts = getvaratts(lineres)
	   do ty = 0,dimsizes(fsatts)-1
	      polyres@$fsatts(ty)$ = lineres@$fsatts(ty)$
	   end do
	end if
	
	if (boxOpts) then
	   if (isatt(boxOpts,"boxColors")) then
	      boxcolor = boxOpts@boxColors
	      if (dimsizes(boxcolor).eq.1.or.dimsizes(boxcolor).ne.numbox) then
	         if (dimsizes(boxcolor).ne.numbox) then
	            print("warning:Number of colors provided does not match number of boxes, using only one color")
		 end if
	         polyres@gsLineColor = boxcolor(0)
	         cflag = 1
	      else
	         cflag = 2
	      end if
	   else
	      cflag =1
	   end if
	else
	   cflag = 1
	end if
	
   ;dum = new((/numbox,9/),graphic)
	dum = new((/numbox,10/),graphic)
	
    dumline = new(3,graphic)  
  
	do gg = 0,numbox-1

           ;------------------------------
           ; first draw the ref. polygon
           ;------------------------------
           if (gg.eq.0) then
           
           ;---------------------------------
           ; mBq/m3 STP <- ion pair/cm3/s STP
           ;---------------------------------

           prd1 = 1.5 / ( 0.001 * 6.4e+05 * 1.0e-06 )
           prd2 = 2.5 / ( 0.001 * 6.4e+05 * 1.0e-06 )
           xxmin = min(xAxis)-1
           xxmax = max(xAxis)+1

           yyp = (/prd1,prd1,prd2,prd2,prd1/)
           xxp = (/xxmin,xxmax,xxmax,xxmin,xxmin/)

           plygonres = True
           plygonres@gsFillColor = 28 
	       dum(gg,0) = gsn_add_polygon(wks,plot,xxp,yyp,plygonres)
	       
           end if

           ;------------------------------
           ; legend
           ;------------------------------

           if (False) then

           ;------------------------------
           ; now draw the polyline  
           ;------------------------------
           
              pline                  = True   ; polyline mods desired
              pline@gsLineThicknessF = 1.0    ; thickness of lines 

              ya = 0.96 ;;(max(y)+2)*1.20
              aa = 0.94 ;;(max(y)+2)*1.12
              bb = 0.96 ;;(max(y)+2)*1.04
              cc = 0.94 ;;(max(y)+2)*0.96
              
              xa = 0.38
              xb = 0.43
              
              xpts = (/ xa, xb /)
              ypts = (/ ya, ya /)
              pline@gsLineColor = boxcolor(0)
              gsn_polyline_ndc(wks,xpts(0:1),ypts(0:1),pline)
              
              xpts = (/ xa, xb /)
              ypts = (/ aa, aa /)
              pline@gsLineColor = boxcolor(1) 
              gsn_polyline_ndc(wks,xpts(0:1),ypts(0:1),pline) 
             
              xa = 0.55
              xb = 0.60
              
              xpts = (/ xa, xb /)
              ypts = (/ bb, bb /)
              pline@gsLineColor = boxcolor(2)
              gsn_polyline_ndc(wks,xpts(0:1),ypts(0:1),pline)    
             
              xpts = (/ xa, xb /)
              ypts = (/ cc, cc /)
              pline@gsLineColor = boxcolor(3)
              gsn_polyline_ndc(wks,xpts(0:1),ypts(0:1),pline)    
               
           ;------------------------------
           ; now add text 
           ;------------------------------
           
              txres               = True
             ;txres@txFont        = "helvetica" 
             ;txres@txFontThicknessF = 1.0 
             ;txres@txFontQuality = "High" 
              txres@txFontHeightF = 0.01
             ;txres@amJust        = "BottomLeft"
             ;txres@amParallelPosF= -0.5   ; Right-justifed 
             
              xb = 0.43 + 0.045
              gsn_text_ndc (wks,"OBS",           xb, ya,txres) 
              gsn_text_ndc (wks,"WCRP1995",      xb, aa,txres) 
              
              xb = 0.60 + 0.06
              gsn_text_ndc (wks,"SW1998 scaled", xb, bb,txres) 
              gsn_text_ndc (wks,"Merged",        xb, cc,txres) 
   
           ;------------------------------
           ; now draw the polygon (box) 
           ;------------------------------
           
              cirx = (/ 0.37, 0.37, 0.72, 0.72, 0.37 /)
              ciry = (/ 0.92, 0.98, 0.98, 0.92, 0.92 /) 

              gsres = True                    ; Indicate you want to set some resources.
              gsres@gsLineThicknessF = 0.5    ; thickness of lines
              gsres@gsFillColor      = -1     ; Change fill color.
              gsres@gsEdgesOn        = True   ; Change fill color.
              gsres@gsEdgeColor      = "grey" ; Change fill color.

              gsn_polygon_ndc(wks,cirx,ciry,gsres)  ; Draw a filled polygon.
  
             ;drawNDCGrid(wks)
              
           end if

       ;------------------------------
       ; now the boxes
       ;------------------------------
       
	   ff = xAxis(gg+1)
	   if (cflag.eq.2) then
	       polyres@gsLineColor = boxcolor(gg)
	   end if
	   yy = (/y(gg,4),y(gg,4)/)
	   xx = (/(ff-(boxwidth/8.)),(ff+(boxwidth/8.))/)
	   dum(gg,1) = gsn_add_polyline(wks,plot,xx,yy,polyres)   

	   yy = (/y(gg,3),y(gg,4)/)
	   xx = (/ff,ff/)
	   polyres@gsLineDashPattern = 1
	   dum(gg,2) = gsn_add_polyline(wks,plot,xx,yy,polyres)   
	   polyres@gsLineDashPattern = 0

	   yy = (/y(gg,3),y(gg,3)/)
	   xx = (/(ff-(boxwidth/2.)),(ff+(boxwidth/2.))/)
	   dum(gg,3) = gsn_add_polyline(wks,plot,xx,yy,polyres)

	   yy = (/y(gg,1),y(gg,3)/)
	   xx = (/(ff-(boxwidth/2.)),(ff-(boxwidth/2.))/)
	   dum(gg,4) = gsn_add_polyline(wks,plot,xx,yy,polyres)   

	   yy = (/y(gg,2),y(gg,2)/)
	   xx = (/(ff-(boxwidth/2.)),(ff+(boxwidth/2.))/)
	   dum(gg,5) = gsn_add_polyline(wks,plot,xx,yy,polyres)   

	   yy = (/y(gg,1),y(gg,3)/)
	   xx = (/(ff+(boxwidth/2.)),(ff+(boxwidth/2.))/)
	   dum(gg,6) = gsn_add_polyline(wks,plot,xx,yy,polyres)   

	   yy = (/y(gg,1),y(gg,1)/)
	   xx = (/(ff-(boxwidth/2.)),(ff+(boxwidth/2.))/)
	   dum(gg,7) = gsn_add_polyline(wks,plot,xx,yy,polyres)   

	   yy = (/y(gg,0),y(gg,1)/)
	   xx = (/ff,ff/)
	   polyres@gsLineDashPattern = 1
	   dum(gg,8) = gsn_add_polyline(wks,plot,xx,yy,polyres)   
	   polyres@gsLineDashPattern = 0

	   yy = (/y(gg,0),y(gg,0)/)
	   xx = (/(ff-(boxwidth/8.)),(ff+(boxwidth/8.))/)
	   dum(gg,9) = gsn_add_polyline(wks,plot,xx,yy,polyres)

	end do

	dumname = unique_string("dum")								
	plot@$dumname$ = dum									
	if (isvar("xblab").eq."True") then							
	   plotres@tmXBLabels = xblab		;reassign XBLabels for possible future plots	
	end if				
	if(maximize) then
          mres = True
          mres@gsnDraw  = False
          mres@gsnFrame = False
          maximize_output(wks,mres)
        end if

        return(plot)

end
; ---------------------------------

